package com.xy.service.impl;

import java.util.List;

import com.xy.dao.IBaseDao;
import com.xy.dao.impl.IBaseDaoImpl;
import com.xy.dao.impl.PageResults;
import com.xy.model.PayMent;
import com.xy.model.StockReceive;
import com.xy.model.Supplier;
import com.xy.model.TextBook;
import com.xy.service.MyInterface;

public class StockReceiveServiceImpl implements MyInterface {
	IBaseDao dao = new IBaseDaoImpl();

	@Override
	public boolean add(Object object) {
		// TODO Auto-generated method stub
		try {
			dao.save(object);
			PayMent pay = new PayMent();
			pay.setStockReceive((StockReceive) object);
			dao.save(pay);
			dao.commit();
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("StockReceive:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean update(Object object) {
		// TODO Auto-generated method stub
		try {
			dao.update(object);
			dao.commit();
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return false;
		}
	}

	@Override
	public boolean delete(Object object) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> Object get(int id) {
		// TODO Auto-generated method stub
		return dao.get(StockReceive.class, id);
	}

	@Override
	public PageResults listByPage(int page) {
		// TODO Auto-generated method stub
		return dao.getPageByFetchedHql("from StockReceive", "select count(*) from StockReceive", page + 1, 10);
	}

	@Override
	public <T> List<Object> list(Class<T> name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults query(Object... objects) {
		// TODO Auto-generated method stub

		String supplier = null;
		String textBook = null;
		String start = null;
		String end = null;
		int pageno = 0;
		if (objects != null) {
			start = (String) objects[0];
			end = (String) objects[1];
			supplier = (String) objects[2];
			textBook = (String) objects[3];
			pageno = (Integer) objects[4];
		}

		return dao.getPageByFetchedHql(
				"from StockReceive where " + "supplier.name like '%" + supplier + "%'" + " and textBook.name like '%"
						+ textBook + "%'" + " and dateTime >='" + start + "' " + " and dateTime <='" + end + "'",
				"select count(*) from StockReceive where " + "supplier.name like '%" + supplier + "%'"
						+ " and textBook.name like '%" + textBook + "%'" + "and dateTime >='" + start + "' "
						+ "and dateTime <='" + end + "'",
				pageno + 1, 10);
	}

	public List<Object> getByObject(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	public PageResults queryFromMove(Object... objects) {
		// TODO Auto-generated method stub

		String cell = null;
		String textBook = null;
		int pageno = 0;
		if (objects != null) {
			cell = (String) objects[0];
			textBook = (String) objects[1];
			pageno = (Integer) objects[2];
		}

		return dao.getPageByFetchedHql(
				"from StockReceive where " 
						+ "textBook.name like '%" + textBook + "%'"
						+ "and textBook.cell.name like '%" + cell + "%'",
				"select count(*) from StockReceive where " 
						+ "textBook.name like '%" + textBook + "%'"
						+ "and textBook.cell.name like '%" + cell + "%'",
				pageno + 1, 5);
	}

}
